package com.zjm.servlet.servlet05_config_context;

import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @author ZJM
 * @date 2024/4/11 22:36
 * ServletContext对象的作用
 */
public class ServletContextTest01Servlet extends HttpServlet {

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        ServletContext servletContext = getServletConfig().getServletContext();

        //1.获取web.xml配置文件的context-param
        System.out.println("context参数："+servletContext.getInitParameter("username"));
        System.out.println("context参数："+servletContext.getInitParameter("password"));
        //2.获取当前项目的工程路径
        System.out.println("获取当前项目的工程路径："+servletContext.getContextPath());
        //3.获取当前项目部署在硬盘上的绝对路径
        System.out.println("获取当前项目部署在硬盘上的绝对路径："+servletContext.getRealPath("/"));
        System.out.println("获取当前项目部署在硬盘上的绝对路径css："+servletContext.getRealPath("/css"));
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
    }
}
